package com.hui.config.security.filter;

import com.alibaba.fastjson.JSONObject;
import com.hui.config.security.constant.AuthError;
import com.hui.config.security.utils.CrosUtil;
import com.hui.dto.JsonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拒绝访问处理
 */
public class RestAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(AuthError.NO_PERMISSION.msg);
        jsonResult.setCode(AuthError.NO_PERMISSION.code);
        CrosUtil.crosOrgin(request, response);
        response.getWriter().write(JSONObject.toJSONString(jsonResult));
    }
}
